package com.cljh.controller;

import com.cljh.bean.Admin;
import com.cljh.bean.Role;
import com.cljh.controller.page.Code;
import com.cljh.controller.page.PageBean;
import com.cljh.controller.page.Result;
import com.cljh.service.RoleService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/roles")
public class RoleController {
    @Autowired
    RoleService roleService;

    @PostMapping
    public Result save(@RequestBody Role role) {
        boolean save = roleService.save(role);
        Integer code = save ? Code.SAVE_OK : Code.SAVE_ERR;
        String msg = save ? "添加成功" : "添加失败";
        return new Result(code, save, msg);
    }

    @DeleteMapping("/{id}")
    public Result delete( @PathVariable Integer id) {
        boolean delete = roleService.delete(id);
        Integer code=delete ? Code.DELETE_OK:Code.DELETE_ERR;
        String msg=delete ? "删除一个成功":"删除一个失败";
        return new Result(code,delete,msg);
    }

    @PutMapping
    public Result Upd(@RequestBody Role role) {
        boolean update = roleService.Upd(role);
        Integer code=update ? Code.UPDATE_OK:Code.UPDATE_ERR;
        String msg=update ? "修改成功":"修改失败";
        return new Result(code,update,msg);
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        Role role = roleService.selectById(id);
        Integer code=role !=null ? Code.GET_OK:Code.GET_ERR;
        String msg=role!=null ? "查询到信息":"未查询到信息";
        return new Result(code,role,msg);
    }

    @GetMapping("/get")
    public Result selectAll(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "5") Integer pageSize, @RequestParam(defaultValue = "") String name,@RequestParam(defaultValue = "") String description,@RequestParam(defaultValue = "") String key) {

//        System.out.println(currentPage);
//        System.out.println(pageSize);
//        System.out.println(name);
//        System.out.println(description);
        Role role = new Role(null, name, description,key);

        System.out.println(role);
        PageBean<Role> rolePageBean = roleService.selectAll(currentPage, pageSize, role);

        Integer code=rolePageBean !=null ? Code.GET_OK:Code.GET_ERR;
        String msg=rolePageBean!=null ? "查询到信息":"未查询到信息";
        return new Result(code,rolePageBean,msg);
    }


    @DeleteMapping("/deleteIds")
    public Result deleteByIds(@RequestBody List<Integer> ids){
        boolean b = roleService.deleteIds(ids);
        Integer code=b ? Code.DELETE_OK:Code.DELETE_ERR;
        String msg=b ? "删除成功":"删除失败";
        return new Result(code,b,msg);
    }

    /**
     * 绑定角色和菜单的关系
     * @param roleId 角色id
     * @param menuIds 菜单id数组
     * @return
     */
    @PostMapping("/roleMenu/{roleId}")
    public Result roleMenu(@PathVariable Integer roleId ,@RequestBody List<Integer> menuIds){
//        System.out.println(roleId);
        roleService.setRoleMenu(roleId,menuIds);
        return new Result(Code.UPDATE_OK,null,"修改成功");
    }


    @GetMapping("/roleMenu/{roleId}")
    public Result getRoleMenu(@PathVariable Integer roleId){
        List<Integer> roleMenu = roleService.getRoleMenu(roleId);
        Integer code=roleMenu !=null ? Code.GET_OK:Code.GET_ERR;
        String msg=roleMenu!=null ? "查询到信息":"未查询到信息";
        return new Result(code,roleMenu,msg);
    }

    @GetMapping("/all")
    public Result selectAll(){
        List<Role> roles = roleService.selectAll1();
        Integer code=roles !=null ? Code.GET_OK:Code.GET_ERR;
        String msg=roles!=null ? "查询到信息":"未查询到信息";
        return new Result(code,roles,msg);
    }


}
